﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

using MaterialSkin;
using MaterialSkin.Controls;


namespace home_material_tool
{
    public partial class SettingForm : MaterialForm
    {

        MaterialSnackBar snackBar = default;


        //private const string DB = "hm_video";
        private string connectioinStr = string.Empty;
        
        public SettingForm()
        {
            InitializeComponent();

            //Common.mysqlConn.StateChange += new StateChangeEventHandler(OnStateChange);

            Common.MSkinManager.AddFormToManage(this);

            InitData();



        }
        //https://learn.microsoft.com/zh-cn/dotnet/framework/data/adonet/connection-events?redirectedfrom=MSDN
        //private void OnStateChange(Object sender, StateChangeEventArgs e)
        //{
        //    if (e.CurrentState == ConnectionState.Closed && bTestCon)
        //    {
        //        connectioinStr = string.Format("server={0};uid={1};pwd={2};database={3};charset=utf8mb4;",
        //        mTxtIP.Text.Trim(), mTxtUser.Text.TrimStart().TrimEnd(), mTxtPass.Text, DB);
        //        try
        //        {
        //            Common.mysqlConn.ConnectionString = connectioinStr;
        //            Common.mysqlConn.OpenAsync();
        //        }
        //        catch (MySqlException ex)
        //        {
        //            MessageBox.Show(ex.Message);
        //        }

        //    }
        //    else if (e.CurrentState == ConnectionState.Open && bTestCon)
        //    {
        //        bTestCon = false;
        //        MessageBox.Show("OK");
        //    }
        //}

        private void InitData()
        {
            if(!string.IsNullOrEmpty(Properties.Settings.Default.DBParameter))
            {
                string[] str = Properties.Settings.Default.DBParameter.Split(new string[] { "||||" }, 4, StringSplitOptions.None);
                mTxtIP.Text = str[0];
                mTxtPort.Text = str[1];
                mTxtUser.Text = str[2];
                mTxtPass.Text = str[3];
            }
        }
        private void mBtnConnectTest_Click(object sender, EventArgs e)
        {
            //connectioinStr = string.Format("server={0};uid={1};pwd={2};database={3};charset=utf8mb4;",
            //    mTxtIP.Text.Trim(), mTxtUser.Text.TrimStart().TrimEnd(), mTxtPass.Text, DB);
            OpenCloseAsyncDB();

        }

        private async void OpenCloseAsyncDB()
        {
            snackBar = new MaterialSnackBar();
            try
            {
                if (CommonSql.MysqlConn.State != ConnectionState.Closed)
                {
                    //add await just for throwing exception
                    //https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbconnection.openasync?view=net-8.0
                    await CommonSql.MysqlConn.CloseAsync();
                }
                connectioinStr = string.Format("server={0};port={1};uid={2};pwd={3};database={4};charset=utf8mb4;",
                    mTxtIP.Text.Trim(), mTxtPort.Text.Trim(), mTxtUser.Text.TrimStart().TrimEnd(), mTxtPass.Text, CommonSql.DB);
                CommonSql.MysqlConn.ConnectionString = connectioinStr;


                await CommonSql.MysqlConn.OpenAsync();
                //MessageBox.Show("OK");

                snackBar.BarType = MaterialSnackBar.SnackBarType.Suceesss;
                snackBar.Text = "连接成功";
                
            }
            catch(MySqlException ex)
            {
                //MessageBox.Show(ex.Message);
                snackBar.BarType = MaterialSnackBar.SnackBarType.Danger;
                snackBar.Text = ex.Message;
            }
            snackBar.Show(this);

        }



        private void mBtnOK_Click(object sender, EventArgs e)
        {
            CommonSql.ConnectionString = string.Format("server={0};port={1};uid={2};pwd={3};database={4};charset=utf8mb4;",
                    mTxtIP.Text.Trim(), mTxtPort.Text.Trim(), mTxtUser.Text.TrimStart().TrimEnd(), mTxtPass.Text, CommonSql.DB);

            //save the setting data
            Properties.Settings.Default.DBParameter = mTxtIP.Text.Trim() + "||||" + mTxtPort.Text.Trim() + "||||"
                    + mTxtUser.Text.TrimStart().TrimEnd() + "||||" + mTxtPass.Text;
            Properties.Settings.Default.Save();
            this.Close();
        }
    }
}
